<template>
  <chart ref="chart" :options="options" :auto-resize="autoResize" :theme="theme" :watch-shallow="watchShallow"
         :class="className" :style="{height:height,width:width}" @click="onClick"></chart>
</template>

<script>
  const animationDuration = 2000
  export default {
    props: {
      chartData: { type: Array },
      className: { type: String, default: 'chart' },
      theme: { type: String, default: 'macarons' },
      autoResize: { type: Boolean, default: true },
      watchShallow: { type: Boolean, default: true },
      width: { type: String, default: '100%' },
      height: { type: String, default: '500px' }
    },
    data() {
      return {
        options: this.buildOptions(this.charData),
        jobChecked: 0,
        jobStepLength: 0
      }
    },
    watch: {
      chartData: {
        deep: true,
        handler(val) {
          this.options = this.buildOptions(val)
        }
      }
    },
    mounted() {
      if (!this.options && this.chartData) {
        this.options = this.buildOptions(this.chartData)
      }
    },
    methods: {
      buildOptions(chartData) {
        var _this = this
        if (chartData === null) {
          return
        }
        if (!chartData || !chartData.length === 0) {
          return
        }
        this.jobStepLength = chartData.length
        var ladderData = {}
        ladderData.pending = []
        ladderData.category = []
        ladderData.allData = []
        ladderData.finished = []
        ladderData.error = []
        ladderData.discarded = []
        ladderData.running = []
        for (var i = chartData.length - 1; i >= 0; i--) {
          ladderData.category.push(chartData[i].jobStepName)
          ladderData.pending.push('-')
          ladderData.finished.push('-')
          ladderData.error.push('-')
          ladderData.discarded.push('-')
          ladderData.running.push('-')
          var aa = i === chartData.length - 1 ? 0 : ladderData.allData[chartData.length - i - 2] + chartData[chartData.length - (i + 1) - 1].jobStepTime + chartData[chartData.length - (i + 1) - 1].jobStepWaitTime
          ladderData.allData.push(parseFloat(parseFloat(aa).toFixed(2)))
          var status = chartData[i].status.toLowerCase()
          ladderData[status][chartData.length - (i + 1)] = chartData[i].jobStepTime
        }
        return {
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            },
            formatter: function(params) {
              var tar
              if (params[1].value !== '-') {
                tar = params[1]
              } else {
                tar = params[0]
              }
              return tar.name + '<br/>' + tar.seriesName + ' : ' + tar.value + 'ms'
            }
          },
          legend: {
            data: []
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'value',
            axisLine: {
              show: false
            },
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              formatter: '{value} (ms)'
            }
          },
          yAxis: {
            type: 'category',
            axisLine: {
              show: false
            },
            splitLine: {
              show: true
            },
            axisTick: {
              show: false
            },
            data: ladderData.category
          },
          series: [
            {
              name: '辅助',
              type: 'bar',
              stack: '总量',
              itemStyle: {
                normal: {
                  barBorderColor: 'rgba(0,0,0,0)',
                  color: 'rgba(0,0,0,0)'
                },
                emphasis: {
                  barBorderColor: 'rgba(0,0,0,0)',
                  color: 'rgba(0,0,0,0)'
                }
              },
              data: ladderData.allData.reverse()
            },
            {
              name: '准备运行',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'top'
                }
              },
              itemStyle: {
                color: function(params) {
                  var list = ['#009A61', '#F5A623']
                  if ((_this.jobStepLength - params.dataIndex - 1) === _this.jobChecked) {
                    return list[1]
                  } else {
                    return list[0]
                  }
                },
                barBorderRadius: 5
              },
              barWidth: '10px',
              data: ladderData.pending
            },
            {
              name: '完成',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'top'
                }
              },
              itemStyle: {
                color: function(params) {
                  var list = ['#8CC749', '#F5A623']
                  if ((_this.jobStepLength - params.dataIndex - 1) === _this.jobChecked) {
                    return list[1]
                  } else {
                    return list[0]
                  }
                },
                barBorderRadius: 5
              },
              barWidth: '10px',
              data: ladderData.finished
            },
            {
              name: '已中断',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'top'
                }
              },
              itemStyle: {
                color: function(params) {
                  var list = ['#999', '#F5A623']
                  if ((_this.jobStepLength - params.dataIndex - 1) === _this.jobChecked) {
                    return list[1]
                  } else {
                    return list[0]
                  }
                },
                barBorderRadius: 5
              },
              barWidth: '10px',
              data: ladderData.discarded
            },
            {
              name: '进行中',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'top'
                }
              },
              itemStyle: {
                color: function(params) {
                  var list = ['#5DB2FF', '#F5A623']
                  if ((_this.jobStepLength - params.dataIndex - 1) === _this.jobChecked) {
                    return list[1]
                  } else {
                    return list[0]
                  }
                },
                barBorderRadius: 5
              },
              barWidth: '10px',
              data: ladderData.running
            },
            {
              name: '失败',
              type: 'bar',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'top'
                }
              },
              itemStyle: {
                color: function(params) {
                  var list = ['#FB6E52', '#F5A623']
                  if ((_this.jobStepLength - params.dataIndex - 1) === _this.jobChecked) {
                    return list[1]
                  } else {
                    return list[0]
                  }
                },
                barBorderRadius: 5
              },
              barWidth: '10px',
              data: ladderData.error
            }
          ],
          animationDuration
        }
      },
      onClick(event, instance, echarts) {
        this.jobChecked = this.jobStepLength - arguments[0].dataIndex - 1
        this.$emit('editJobStepChecked', arguments[0].dataIndex)
        this.options = this.buildOptions(this.chartData)
      }
    }
  }
</script>
